layui.config({
    base: '/static/layui/'
}).extend({
    tableSelect: 'tableSelect',
}).use(['form', 'layer', 'jquery', 'table', 'laydate', 'upload','tableSelect'], function () {
    const form = layui.form;
    const table = layui.table;
    const tableSelect = layui.tableSelect;
    const $ = layui.jquery;

    form.render()

    table.render({
        elem: '#table'
        ,title: '列表'
        ,method: 'post'
        ,height: 'full-60'
        ,page: true //开启分页
        ,limit: 20
        ,limits: [20, 30, 40, 50, 60, 70, 80, 90, 100]
        ,loading: true
        ,cols: [
            [
                {type: 'checkbox'},
                { field: 'djMaterial', title: '东矩料号'},
                { field: 'cusMaterial', title: '客户料号'},
                { field: 'materialMrk', title: '料号描述' },
                { field: 'typeOfMachine', title: '品名(机种)' },
                { field: 'cusCode', title: '供应商编码' },
                { field: 'cusName', title: '供应商名称' },
                { field: 'classType', title: '存货大类' },
                { field: 'className', title: '存货大类名称' },
            ]
        ]
        ,text: {
            none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
        }
        ,data: []
        ,toolbar: '#tableToolbar'
        ,defaultToolbar: ['filter']
    });

    //监听表格内 行事件
    table.on('row(table)', function(obj){
        var data_index = $(this).attr('data-index');//得到当前的tr的index
        $(".layui-table-body tr").attr({ "style": "background:#FFFFFF; color:#666666" });//其他tr恢复颜色
        $(".layui-table-body tr[data-index=" + data_index + "]").attr({ "style": "background:#999;color:#666666" });
    });

    tableSelect.render({
        elem: '#cusCode',
        checkedKey: 'cusCode',
        searchPlaceholder: '企业编码',
        searchKey: 'cusCode',
        table: {
            url: 'manage/common/list/customer'
            ,method: 'post'
            ,cols: [
                [
                    { type: 'radio' },
                    { field: 'cusCode', title: '企业编码' },
                    { field: 'cusName', title: '企业名称' },
                ]
            ]
        },
        done: function (elem, data) {
            var NEWJSON = []
            var NEWJSONName = []
            layui.each(data.data, function (index, item) {
                NEWJSON.push(item.cusCode)
                NEWJSONName.push(item.cusName)
            })
            elem.val(NEWJSON.join(","))
            $("#cusName").val(NEWJSONName.join(","))
        }
    })

    table.on('toolbar(table)', function(obj){
        switch (obj.event) {
            case 'del':
                let tableCheckData = table.checkStatus("table");
                let ids = new Array();
                $.each(tableCheckData.data, function (index, e) {
                    ids.push(e.id);
                });
                if (!ids.length){
                    layer.msg("请选择", {icon: 2});
                    return;
                }
                top.layer.confirm('是否删除?', {icon: 3, title: '提示'}, function (index) {
                    var shadeIndex = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.4});
                    $.ajax({
                        url: "/base_material/delete/",
                        type: "post",
                        data: JSON.stringify(ids),
                        contentType: "application/json",
                        success: function (d) {
                            if (d.code == '200') {
                                top.layer.alert("删除成功", {icon: 1});
                                table.reload('table');
                            } else {
                                top.layer.alert("删除失败", {icon: 2});
                            }
                            top.layer.close(shadeIndex);
                        },
                        error: function () {
                            top.layer.close(shadeIndex);
                            alert("删除失败！");
                        }
                    });
                    top.layer.close(index);
                });
                break;
            case 'find':
                if($('#cusCode').val() == ''){
                    layer.msg("请选择供应商", {icon: 2});
                    return;
                }
                table.reload('table', {
                    url: 'base_material/djBaseList',
                    where: {
                        cusCode:$('#cusCode').val(),
                        invCode:$('#material').val(),
                        invDef:$('#cInvDefine9').val(),
                    }
                });
                break;
        }
    })

})